package kr.saeildamil.androidstudy.ch12;

import java.util.ArrayList;

import kr.saeildamil.androidstudy.R;
import android.app.Activity;
import android.os.Bundle;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class ListAddDelMulti extends Activity {
	ArrayList<String> arList;
	ArrayAdapter<String> adapter;
	ListView list;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch12_listadddel);
		
		arList = new ArrayList<String>();
		arList.add("First");
		arList.add("Secont");
		arList.add("Third");
		arList.add("Fourth");
		arList.add("Fifth");
		
		adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, arList);
		
		list = (ListView) findViewById(R.id.ch12_listadddel_list);
		list.setAdapter(adapter);
		list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
		list.setOnItemClickListener(listener);
	
	}
	
	AdapterView.OnItemClickListener listener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			Toast.makeText(ListAddDelMulti.this, arList.get(position), Toast.LENGTH_SHORT).show();
			
		}
	};
	
	public void mOnClick(View v){
		EditText et = (EditText) findViewById(R.id.ch12_listadddel_et);
		switch(v.getId()){
		case R.id.ch12_listadddel_addbtn:
			String str = et.getText().toString();
			if(et.length() != 0){
				arList.add(str);
				et.setText("");
				adapter.notifyDataSetChanged();
			}
			break;
		case R.id.ch12_listadddel_delbtn:
			SparseBooleanArray sb = list.getCheckedItemPositions();
			Toast.makeText(ListAddDelMulti.this, "선택한 사이즈" + sb.size(), Toast.LENGTH_SHORT).show();
			if(sb.size() !=0 ){
				for(int i=list.getCount()-1;i>=0;i--){
					if(sb.get(i)){
						arList.remove(i);
					}
				}
				list.clearChoices();
				adapter.notifyDataSetChanged();
			}
			break;
		}
		
		
	}
	
	
}
